<script setup lang="ts">
import { useVbenModal } from '@vben/common-ui';
import {useVbenForm} from "#/adapter/form";
// import {createNewSession} from "#/api/system/ollama/anythingllm";
import {useUserStore} from "@vben/stores";

const emit = defineEmits<{ (sessionCreated: string): void; }>();

const userStore = useUserStore();
const [BasicModal, modalApi] = useVbenModal({
  draggable: true,
  onConfirm: handleSubmit,
  onOpenChange: (isOpen) => {
    if (!isOpen) {
      return null;
    }
    const data = modalApi.getData();
  },
});

async function handleSubmit() {
  try {
    modalApi.modalLoading(true);

    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    const content = await formApi.getValues();
    // await createNewSession({userId: userStore.userInfo?.userId, name: content.name});
    emit('sessionCreated');
    modalApi.close();
  } catch (error) {
    console.error(error);
  } finally {
    modalApi.modalLoading(false);
  }
}

const [BasicForm, formApi] = useVbenForm({
  layout: 'horizontal', // 修改为水平布局
  commonConfig: {
    formItemClass: 'col-span-2',
    componentProps: {
      class: 'w-full',
    },
    labelWidth: 50, // 设置一个合适的标签宽度
  },
  schema: [
    {
      label: '标题',
      fieldName: 'name',
      component: 'Input',
      rules: 'required',
    },
  ],
  showDefaultActions: false,
  wrapperClass: 'grid-cols-2',
});

</script>

<template>
  <BasicModal
      :close-on-click-modal="false"
      :fullscreen-button="false"
      title="新建会话"
      class="w-[500px] h-[260px]"
  >
    <BasicForm />
  </BasicModal>
</template>

<style scoped>

</style>
